package cn.xfdzx.android.apps.shop.presenter;

import cn.xfdzx.android.apps.shop.base.BasePresenter;
import cn.xfdzx.android.apps.shop.bean.CategoryAllBeen;
import cn.xfdzx.android.apps.shop.bean.ShopGoodsInfoBeen;
import cn.xfdzx.android.apps.shop.bean.ShopAddGoodBeen;
import cn.xfdzx.android.apps.shop.bean.ShopAddGoodTypeBeen;
import cn.xfdzx.android.apps.shop.contract.ShopAddGoodContract;
import cn.xfdzx.android.apps.shop.model.ShopAddGoodModel;
import cn.xfdzx.android.apps.shop.net.RxScheduler;
import io.reactivex.functions.Consumer;

/**
 * @author azheng
 * @date 2018/6/4.
 * Description：
 */
public class ShopAddGoodPresenter extends BasePresenter<ShopAddGoodContract.View> implements ShopAddGoodContract.Presenter {

    private ShopAddGoodContract.Model model;

    public ShopAddGoodPresenter() {
        model = new ShopAddGoodModel();
    }


    @Override
    public void addGood(String state,String title, String thumbs, String marketprice, String discountprice, String stock, String category_id, String store_category_id, String goods_weight, String unit) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.addGood(state,title,  thumbs,  marketprice,  discountprice,  stock,  category_id,  store_category_id,  goods_weight,  unit)
                .compose(RxScheduler.<ShopAddGoodBeen>Flo_io_main())
                .as(mView.<ShopAddGoodBeen>bindAutoDispose())
                .subscribe(new Consumer<ShopAddGoodBeen>() {
                    @Override
                    public void accept(ShopAddGoodBeen bean) throws Exception {
                        mView.onAddGoodSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void updataGood(String id, String state, String title, String thumbs, String marketprice, String discountprice, String stock, String category_id, String store_category_id, String goods_weight, String unit) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.updataGood(id,state,title,  thumbs,  marketprice,  discountprice,  stock,  category_id,  store_category_id,  goods_weight,  unit)
                .compose(RxScheduler.<ShopAddGoodBeen>Flo_io_main())
                .as(mView.<ShopAddGoodBeen>bindAutoDispose())
                .subscribe(new Consumer<ShopAddGoodBeen>() {
                    @Override
                    public void accept(ShopAddGoodBeen bean) throws Exception {
                        mView.onUpdataSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }


    @Override
    public void initTypeData(String index_status,int is_tiao) {
        if (!isViewAttached()) {
            return;
        }

        model.initTypeData(index_status)
                .compose(RxScheduler.<CategoryAllBeen>Flo_io_main())
                .as(mView.<CategoryAllBeen>bindAutoDispose())
                .subscribe(new Consumer<CategoryAllBeen>() {
                    @Override
                    public void accept(CategoryAllBeen bean) throws Exception {
                        mView.onInitTypeSuccess(bean,is_tiao);

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);

                    }
                });
    }

    @Override
    public void shopAddGoodType(boolean is_visibility) {
        if (!isViewAttached()) {
            return;
        }

        model.shopAddGoodType( )
                .compose(RxScheduler.<ShopAddGoodTypeBeen>Flo_io_main())
                .as(mView.<ShopAddGoodTypeBeen>bindAutoDispose())
                .subscribe(new Consumer<ShopAddGoodTypeBeen>() {
                    @Override
                    public void accept(ShopAddGoodTypeBeen bean) throws Exception {
                        mView.onShopAddGoodTypeSuccess(bean,is_visibility);

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);

                    }
                });
    }

    @Override
    public void goodsInfo(String id) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.goodsInfo(id )
                .compose(RxScheduler.<ShopGoodsInfoBeen>Flo_io_main())
                .as(mView.<ShopGoodsInfoBeen>bindAutoDispose())
                .subscribe(new Consumer<ShopGoodsInfoBeen>() {
                    @Override
                    public void accept(ShopGoodsInfoBeen bean) throws Exception {
                        mView.onGoodsInfoSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }


}
